/**
* @file        usart.c
* @brief       This file provides all the basic USART firmware functions.
* @author      ChipSea MCU Group
* @version     1.0.0
* @date        2022.04.25
* @copyright   CHIPSEA TECHNOLOGIES (SHENZHEN) CORP.
* @note        
****************************************************************************************************
* @par Modify record:
* <tr><th>Date          <th>Version    <th>Author     <th>Description
* <tr><td>2022.04.25    <td>1.0.0      <td>ChipSea    <td>
* -# 
* </table>
*
****************************************************************************************************
*/


/***************************************************************************************************
 * @includes
***************************************************************************************************/
#include "usart.h"
#include "cs32f10x_usart.h"
#include "cs32f10x_rcu.h"
#include "cs32f10x_gpio.h"

/***************************************************************************************************
 * @definitions
***************************************************************************************************/


/** @addtogroup CS_StdPeriph_Driver
  * @{
  */

/** @defgroup USART USART
  * @brief    USART driver modules
  * @{
  */

///@cond

/***************************************************************************************************
 * @variables
***************************************************************************************************/


/***************************************************************************************************
 * @function
***************************************************************************************************/
/**@brief       Configure USART3 as the printing port.
 *
 * @param[in]   None.
 *
 * @return      None.
 */
void usart_config(void)
{
    usart_config_t ptr_usart;
    
    /* Enable clocks. */
    __RCU_APB2_CLK_ENABLE(RCU_APB2_PERI_GPIOA);
    __RCU_APB2_CLK_ENABLE(RCU_APB2_PERI_USART1);
    __RCU_APB2_CLK_ENABLE(RCU_APB2_PERI_AFIO);
    
    /* Configure PB10(TX) and PB11(RX). */
    gpio_mode_config(GPIOA, GPIO_PIN_9, GPIO_MODE_OUT_AFPP(GPIO_SPEED_HIGH));
    gpio_mode_config(GPIOA, GPIO_PIN_10, GPIO_MODE_IN_PU);
    
    /* Configure the basic information of USART3. */
    usart_def_init(USART1);
    ptr_usart.baud_rate = 115200;
    ptr_usart.data_width = USART_DATA_WIDTH_8;
    ptr_usart.flow_control = USART_FLOW_CONTROL_NONE;
    ptr_usart.parity_check = USART_PARITY_NONE;
    ptr_usart.stop_bits = USART_STOP_BIT_1;
    ptr_usart.transceiver_mode = USART_MODE_TX_RX;
    usart_init(USART1, &ptr_usart);
    
    /* Enable USART3. */
    __USART_ENABLE(USART1);
}

/**@brief       Output print information through USART3.
 *
 * @param[in]   None.
 *
 * @return      None.
 */
int fputc(int ch, FILE *f)
{
    (void) f;
    while(__USART_FLAG_STATUS_GET(USART1, TC) == RESET);
    __USART_DATA_SEND(USART1, (uint8_t) ch);

    return ch;
}

///@endcond

/**
  * @}
  */

/**
  * @}
  */
